/**
 * 家长端
 * 		学生刷卡记录代码
 */


var pageNo = 1;     //当前页
var pageSize = 4;   //每页记录数
var isApply = false;//当前是否查询外出申请，默认查询刷卡记录
var dataJson;       //查询参数
var tabCurrent = 0; //当前标签，进校，出校，异常记录
var islogin = 1;
$(function(){
    $(".fixed-bottom a").eq(0).click(function () {
    	//window.location.href = "/door/front_end/weChat/patriarch/card_weChat_parent.html";
    	isApply = false;
    	//设置"刷卡记录"为激活状态
    	$(".fixed-bottom a").eq(0).addClass("fix-bottom-active");
    	$(".fixed-bottom a").eq(1).removeClass("fix-bottom-active");
    	//显示"刷卡记录"内容区
    	$(".record").css("display","block");
    	$(".apply").css("display","none");
        //查询"出校记录"
    	tabCurrent = 0;
    	query(1);
    });
    $(".fixed-bottom a").eq(1).click(function () {
    	//window.location.href = "/door/front_end/weChat/patriarch/apply_weChat_parent.html";
    	isApply = true;
    	//设置"外出申请"为激活状态
    	$(".fixed-bottom a").eq(1).addClass("fix-bottom-active");
    	$(".fixed-bottom a").eq(0).removeClass("fix-bottom-active");
    	//显示"外出申请"内容区
    	$(".record").css("display","none");
    	$(".apply").css("display","block");
    	//查询"外出申请"记录
    	queryApply(1);
    });
    init();
});

function init(){
	$.ajax({
        url: "/door/front/parent/getCurStu?" + new Date().getTime(),
        async: false,
        success: function (str) {
            if (check(str)) {
                $("#stu_name").text(str.name);
                $("#class_name").text(str.classname);
            }
        }
    });
    setTableBTN();
	query(1);
}
function searTimeClick(){
	pageNo = 1;
	queryData();
}
function setTableBTN() {
	$("#page").unbind();
    $("#page").change(function (i) {
    	pageNo = parseInt($("#page").find("option:selected").text());
    	if(pageNo>Pagination.pageCount||pageNo<1||pageNo==Pagination.pageNo){
    		pageNo=Pagination.pageCount;
        }
    	if(isApply){
    		queryDataApply();
    	}else{
    		queryData();
    	}
    });
    $(".page-nav span").each(function (i) {
        if (i == 1) {//上一页按钮
            this.onclick = function () {
            	pageNo=Pagination.pageNo-1;
            	if(pageNo>Pagination.pageCount||pageNo<1||pageNo==Pagination.pageNo){
                    layer.msg("当前是第一页", {time: 1000});
            		return;
                }
            	if(isApply){
            		queryDataApply();
            	}else{
            		queryData();
            	}
            };
        } else if (i == 2) {//下一页按钮
            this.onclick = function () {
            	pageNo=Pagination.pageNo+1;
            	if(pageNo>Pagination.pageCount||pageNo<1||pageNo==Pagination.pageNo){
                    layer.msg("当前是最后一页", {time: 1000});
                    return;
                }
            	if(isApply){
            		queryDataApply();
            	}else{
            		queryData();
            	}
            };
        } else if (i == 0) {
            this.onclick = function () {
            	pageNo=1;
            	if(isApply){
            		queryDataApply();
            	}else{
            		queryData();
            	}
            };
        } else if (i == 3) {
            this.onclick = function () {
            	pageNo=Pagination.pageCount;
            	if(isApply){
            		queryDataApply();
            	}else{
            		queryData();
            	}
            };
        }
    });
}


//--------------------------刷卡记录开始------------------------------
/**
 * 刷卡记录	查询方法一
 * @param type
 */
function query(type){
	if(type==tabCurrent){
        return;
    }
	pageSize = 5;
	pageNo = 1;
    tabCurrent=type;
	queryData();
}
/**
 * 刷卡记录	查询方法二
 */
function queryData() {
	var sdate = null;
    var edate = null;
    if (tabCurrent == 1) {
        sdate = $("#date-out-start").val();
        edate = $("#date-out-end").val();
    } else if (tabCurrent == 2) {
        sdate = $("#date-in-start").val();
        edate = $("#date-in-end").val();
    } else if (tabCurrent == 3) {
        sdate = $("#date-unusual-start").val();
        edate = $("#date-unusual-end").val();
    }
    if (sdate == "") {
        sdate = "1970-01-01 00:00:00";
    }
    if (edate == "") {
        edate = "2160-01-01 00:00:00";
    }
    var startDate = new Date(sdate.replace("T"," "));
    var endDate = new Date(edate.replace("T"," "));
    dataJson = {
    	startDate: DateUtils.format(startDate, "yyyy-MM-dd HH:mm:ss"),
        endDate: DateUtils.format(endDate, "yyyy-MM-dd HH:mm:ss"),
        pageNo : pageNo,
        pageSize : pageSize,
        type: tabCurrent
    };
    getDataOfRecord();
}

/**
 * 刷卡记录	获取后台数据
 * 带参分页查询函数
 */
function getDataOfRecord() {
    $.ajax({
        async:true,
        type:'POST',
        url:'/door/front/parent/record',
        data:dataJson,
        cache:false,
        dataType : 'json',
        contentType:'application/x-www-form-urlencoded',
        beforeSend:function (xhr) { },
        success:function (data,textStatus,xhr) {
        	if(check(data)){
        		setPageCode(data.itemCount,null,'wx');
            	setDataOfList(data.list);
        	}
        },
        error:function (xhr,textStatus,errorInfo) { },
        complete:function (xhr,textStatus) {}
    });
}
/**
 * 刷卡记录	把后台数据添加到前台页面
 * @param records
 */
function setDataOfList(records) {
    var str = '';
    for(var i=0;i<records.length;i++){
        str += "<tr>";
        str += "<td>" + records[i].stuName + "</td>"; 
        if(tabCurrent==3){
        	var typeIO = records[i].typeIO==0?'进校':'出校';
        	str += "<td>" + typeIO + "</td>"; 
        }
        str += "<td>" + records[i].recordDate + "</td>"; 
        str += "</tr>";
    }
    $(".card-record-table").eq(tabCurrent-1).html(str);
}
//--------------------------刷卡记录结束------------------------------


//--------------------------外出申请开始------------------------------
/**
 * 外出申请	查询方法一
 * @param type
 */
function queryApply(type){
	pageNo = 1;
	pageSize = 3;
	queryDataApply();
}
/**
 * 外出申请	查询方法二
 */
function queryDataApply() {
    dataJson = {
        pageNo : pageNo,
        pageSize : pageSize,
        type: 0
    };
    getDataOfApply();
}
/**
 * 外出申请	获取后台数据
 * 带参分页查询函数
 */
function getDataOfApply() {
    $.ajax({
        async:true,
        type:'POST',
        url:'/door/front/parent/apply',
        data:dataJson,
        cache:false,
        dataType : 'json',
        contentType:'application/x-www-form-urlencoded',
        beforeSend:function (xhr) { },
        success:function (data,textStatus,xhr) {
        	if(check(data)){
        		setPageCode(data.itemCount,null,'wx');
            	setDataOfListApply(data.list);
        	}
        },
        error:function (xhr,textStatus,errorInfo) { },
        complete:function (xhr,textStatus) {}
    });
}
/**
 * 外出申请	把后台数据添加到前台页面
 * @param records
 */
function setDataOfListApply(apply) {
    var str = '';
    for(var i=0;i<apply.length;i++){
        str += "<a href='#'><table class='apply-table-we text-center'><tr>";
        str += "<td class='apply-td1'><p>" +apply[i].className + "</p><p>" + apply[i].stuName + "</p></td>";
        str += "<td class='apply-td2'><p>"+apply[i].startTime+"</p><p>至</p><p>" + apply[i].endTime + "</p></td>";
        str += "<td class='apply-td3'><label>请假原因：</label><p class='apply-reason'>"+apply[i].reasonReq+"</p></td>";
        if(apply[i].spStatus==0){
        	str += "<td class='apply-td4'><p class='state-ing'>审批中</p></td>";
        }
        if(apply[i].spStatus==1){
        	str += "<td class='apply-td4'><p class='state-yes'>审批通过</p></td>";
        }
        if(apply[i].spStatus==2){
        	str += "<td class='apply-td4'><p class='state-no'>审批驳回</p></td>";
        }
        if(apply[i].spStatus==3){
        	str += "<td class='apply-td4'><p class='state-cancel'>撤销</p></td>";
        }
        str += "</tr></table></a>";
    }
    $(".apply").html(str);
}
//--------------------------外出申请结束------------------------------


/**
 * 检查微信用户是否登陆
 * @param str
 * @returns {Boolean}
 */
function check(str){
	if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage, {time: 1000});
                    window.location.href = "/door/front_end/weChat/login_weChat.html";
                    islogin = 0;
                }
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}
